import * as requirUrls from '../../utils/allUrls'
import * as commonFunc from '../../utils/commonFunc'
import {
	register
} from "../../api/login.js";
export default {
	name: "reg",
	data() {
		//注册用户名自定义校验规则
		const usernameCheck = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请输入用户名'));
			} else {
				if (value.length < 4) {
					callback(new Error('用户名长度不能小于4位'));
				} else {
					this.checkSameUser(value, callback, []);
				}
			}
		};
		const nameCheck = (rule, value, callback) => {
			if (value === '') {
				callback(new Error("请输入昵称"))
			} else if (value.length < 2 || value.length > 8) {
				callback(new Error("用户昵称长度只能在2~8位"));
			} else {
				this.checkSameName(value, callback, []);
			}
		};
		return {
			form: {
				userName: '',
				password: ''
			},
			//表单验证
			rules: {
				userName: [{
						required: true,
						trigger: "blur",
						message: "请输入您的账号"
					},
					{
						min: 2,
						max: 20,
						message: '用户账号长度必须介于 2 和 20 之间',
						trigger: 'blur'
					}
				],
				password: [{
						required: true,
						trigger: "blur",
						message: "请输入您的密码"
					},
					{
						min: 5,
						max: 20,
						message: '用户密码长度必须介于 5 和 20 之间',
						trigger: 'blur'
					}
				],
			},
		}

	},
	methods: {
		toHome() {
			this.$router.push('/')
		},
		//    次级选择器-存入数据
		handleChange(val) {
			this.form.address = val[1];
		},
		//检查是否有同个用户
		checkSameUser(value, callback, errors) {
			requirUrls.getUserInfo({
				username: this.form.username,
			}, 'post').then(res => {
				return res.json();
			}).then(res => {
				if (+res.status === 200 && res.data.length > 0) {
					if (res.data[0].username == this.form.username) {
						errors.push("已存在相同用户！");
						callback(new Error(errors));
					}
				} else {
					callback();
				}
			})
		},
		//检查是否有相同用户昵称
		checkSameName(value, callback, errors) {
			requirUrls.getUserInfoByName({
				name: this.form.name
			}, 'post').then(res => {
				return res.json();
			}).then(res => {
				if (+res.status === 200) {
					if (res.data.name === this.form.name) {
						errors.push("已存在相同昵称！");
						callback(new Error(errors));
					}
				} else {
					callback()
				}
			})
		},
		//点击注册
		doReg(regForm) {
		
			register(this.form).then(response => {
			
				this.$alert("<font color='red'>恭喜你，您的账号 " + this.form.userName + " 注册成功！</font>", '系统提示', {
				  dangerouslyUseHTMLString: true
				}).then(() => {
				  this.$router.push("/login");
				}).catch(() => {});
			});
		},
		//注册动态信息
		_insertSpaceInfo() {
			let time = commonFunc.getTime();
			requirUrls.insertSpaceInfo({
				username: this.form.username,
				name: this.form.name,
				date: time,
				content: "在本站注册了账号！~",
			}, 'post').then(res => {
				return res.json();
			}).then(res => {
				if (+res.status === 200) {
					console.log('注册动态信息插入成功！');
				} else {
					this.$message.error("注册动态信息插入失败！");
					console.log("注册动态信息插入失败！")
				}
			}).catch(err => {
				console.log(err);
			})
		},
		//替换空格
		replaceSpace() {
			this.form.name = this.form.name.replace(/\s+/g, "");
		},
		//替换非法字符
		replaceIllegal() {
			this.form.password = this.form.password.replace(/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im, '');
		}
	},
	mounted() {},
	watch: {
		username() {
			this.form.username = this.form.username.replace(/[\W]/g, '');
		}
	},
	computed: {
		username() {
			return this.form.username
		}
	}
}
